//
//  MYCloudDiskHeaderActionView.swift
//  MYDoctor
//
//  Created by Denny on 2024/3/8.
//

import UIKit

class MYCloudDiskHeaderActionView: UIView {
    
    var serachBView: UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(hexString: "#F5F5F5")
        control.addCorner(radius: 17.5)
        return control
    }()
    
    var tapSearchCallBack:((String)->Void)?
//    var searchIcon: UIImageView = {
//        let control = UIImageView()
//        control.image =
//    }()
//    var searchLB: UILabel = {
//        let control = UILabel()
//        control.text = "搜索网盘"
//        control.font = UIFont.regular(14)
//        control.textColor = UIColor.init(hexString: "#8E8E93")
//        return control
//    }()
    lazy var searchTextField: UITextField = {
        let control = UITextField()
        control.textAlignment = .center
        control.placeholder = "搜索网盘"
        control.clearButtonMode = .whileEditing
        control.returnKeyType = .search
        control.delegate = self
        return control
    }()
    var buttons = [UIButton]()
    //下载按钮
    lazy var downloadBtn: UIButton = {
        let control = UIButton()
        control.setImage(UIImage.init(named: "MYG_clouldDisk_download"), for: [])
        control.addTarget(self, action: #selector(tapDownloadAction), for: .touchUpInside)
        return control
    }()
    
    var redLB:UILabel = {
        let control = UILabel()
        control.backgroundColor = UIColor.init(hexString: "#FF0000")
        control.textColor = .white
        control.font = .medium(7)
        control.isHidden = true
        control.textAlignment = .center
        control.layer.cornerRadius = 6
        control.layer.masksToBounds = true
        return control
    }()
    
    var tapIndexCompeltion:((Int)->Void)?
    var tapDownloadCompeltion:(()->Void)?
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(serachBView)
        serachBView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16.5)
            make.right.equalToSuperview().offset(-16.5)
            make.top.equalToSuperview()
            make.height.equalTo(35)
        }
        serachBView.addSubview(searchTextField)
        searchTextField.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.top.bottom.equalToSuperview()
        }
        let titles = ["全部","图片","视频","文件"]
        let buttonWidth = 58
        let marginLeft = 15
        let midMrgin = 23
        for (i,title) in titles.enumerated() {
            let button = UIButton.init(frame: CGRect.init(x: marginLeft + (buttonWidth + midMrgin) * i, y: 53, width: buttonWidth, height: 21))
            button.setTitle(title, for: .normal)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 12)
            button.addTarget(self, action: #selector(tapButton(_ :)), for: .touchUpInside)
            button.tag = i
            button.addCorner(radius: 3)
            addSubview(button)
            buttons.append(button)
        }
        if let first = buttons.first {
            tapButton(first)
        }
        
        addSubview(downloadBtn)
        downloadBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-20)
            make.size.equalTo(CGSize.init(width: 35, height: 35))
            make.top.equalTo(serachBView.snp.bottom).offset(10)
        }
        addSubview(redLB)
        redLB.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-20)
            make.size.equalTo(CGSize.init(width: 12, height: 12))
            make.top.equalTo(serachBView.snp.bottom).offset(10)
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(reloadRedLB), name: Notification.Name("MYCloudDiskHeaderActionView_reloadRedLB"), object: nil)
    }
    
    @objc func reloadRedLB(){
        let count = MYGUploadManager.shared.uploadingArray.count + MYGDownloadManager.shared.downlodingArray.count
        self.redLB.isHidden = count == 0
        self.redLB.text = "\(count)"
    }
     
    func tapFirstBtn() {
        if let first = buttons.first {
            tapButton(first)
        }

    }
    @objc func tapButton(_ sender:UIButton) {
        for button in buttons {
            if button.tag == sender.tag {
                button.backgroundColor = UIColor.init(hexString: "#557EED")
                button.setTitleColor(.white, for: .normal)
            }else {
                button.backgroundColor = UIColor.init(hexString: "#F5F5F5")
                button.setTitleColor(UIColor.init(hexString: "#565656"), for: .normal)
            }
        }
        if let tap = tapIndexCompeltion {
            tap(sender.tag)
        }
    }
    
    @objc func tapDownloadAction() {
        if let tap = tapDownloadCompeltion {
            tap()
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


extension MYCloudDiskHeaderActionView: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if let c = self.tapSearchCallBack {
            searchTextField.resignFirstResponder()
            c(textField.text ?? "")
        }
        return true
    }
}
